package com.yutel.system.struts.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yutel.component.ExceptionUtils;
import com.yutel.system.repository.SysMenu;
import com.yutel.system.service.ISysMenuService;
import com.yutel.system.service.impl.SysMenuService;
import com.yutel.system.struts.core.BaseAction;
import com.yutel.system.struts.core.Page;

public class SysMenuAction extends BaseAction {

	public String tree() {
		String id = (String) getRequest().getParameter("id");
		if(id==null || "".equals(id)){
			id="1";
		}
		List<Map<String, Object>> retList = new ArrayList<Map<String, Object>>();
		try {
			List<SysMenu> list = menuService.list(id);
			for (SysMenu sc : list) {
				Map<String, Object> tree = new HashMap<String, Object>();
				// 节点的id和名称
				tree.put("id", sc.getId());
				tree.put("text",sc.getName());
				tree.put("url", sc.getUrl());
				tree.put("type", sc.getType());
				if(menuService.list(sc.getId().toString()).size()>0){
					tree.put("leaf",false);
				} else {
					tree.put("leaf",true);
				}
				retList.add(tree);
			}
			treeList=retList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	public String list() {
		try {
			page=new Page();
			preparePage(page);
			String id=getRequest().getParameter("id");
			page.setRoot(menuService.list(id));
			success=true;
			return SUCCESS;
		} catch (Exception e) {
			msg=ExceptionUtils.handleException(e);
			return ERROR;
		}
	}
	public String save() {
		try {
			msg=menuService.save(menu);
			success=true;
			return SUCCESS;
		} catch (Exception e) {
			msg=ExceptionUtils.handleException(e);
			return ERROR;
		}
	}
	public String view() {
		try {
			String id=getRequest().getParameter("id");
			menu=menuService.view(id);
			success=true;
			return SUCCESS;
		} catch (Exception e) {
			msg=ExceptionUtils.handleException(e);
			return ERROR;
		}
	}
	public String drop() {
		try {
			String id=getRequest().getParameter("id");
			msg=menuService.drop(id);
			success=true;
			return SUCCESS;
		} catch (Exception e) {
			msg=ExceptionUtils.handleException(e);
			return ERROR;
		}
	}
	private static final long serialVersionUID = 1L;
	private ISysMenuService menuService;
	private SysMenu menu=new SysMenu();
	List<Map<String, Object>> treeList = null;
	private Page page;
	private boolean success;
	private String msg;
	public void setMenuService(SysMenuService menuService) {
		this.menuService = menuService;
	}

	public List<Map<String, Object>> getTreeList() {
		return treeList;
	}

	public void setTreeList(List<Map<String, Object>> treeList) {
		this.treeList = treeList;
	}
	public SysMenu getMenu() {
		return menu;
	}
	public void setMenu(SysMenu menu) {
		this.menu = menu;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
}